using UnityEngine;
using System.Collections;

public class BSPRectangle 
{
    public int x;
    public int y;
    public int width;
    public int height;
    public int right;
    public int bottom;
    public RectangleType rectType;

    public enum RectangleType
    {
        Room,
        Corridor
    }

	public BSPRectangle(int x, int y, int width, int height, RectangleType rType)
	{
        
		SetRectangle(x,y,width,height,rType);
	}

    public BSPRectangle(int x, int y, int width, int height)
    {

        SetRectangle(x, y, width, height, RectangleType.Room);
    }
	
	public void SetRectangle(int x, int y, int width, int height, RectangleType rType)
	{
		this.x =  x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.right = x + width;
		this.bottom = y + height;
        this.rectType = rType;
	}

    public void UpdateRectangle()
    {
        bottom = y + height;
        right = x + width;
    }

    public UnityEngine.Rect GetUnityRectangle()
    {
        return new Rect(x, y, width, height);
    }
	
	#region "Intersection and Contain Code"
	public bool Intersects(BSPRectangle other)
    {        
        int iLeft = (int)Mathf.Max(this.x, other.x);
        int iRight = (int)Mathf.Min(this.right, other.right);
        int iTop = (int)Mathf.Max(this.y, other.y);
        int iBottom = (int)Mathf.Min(this.bottom, other.bottom);
        
        return (iRight > iLeft && iBottom > iTop);
    }
    
    public bool Contains(BSPRectangle other)
    {
        bool pointOne = false;
        bool pointTwo = false;
        bool pointThree = false;
        bool pointFour = false;
        
        if(other.x > this.x &&
           other.x < this.right &&
           other.y > this.y &&
           other.y < this.bottom) pointOne = true;
           
        if(other.right > this.x &&
           other.right < this.right &&
           other.y > this.y &&
           other.y < this.bottom) pointTwo = true;
           
                      
        if(other.x > this.x &&
           other.x < this.right &&
           other.bottom > this.y &&
           other.bottom < this.bottom) pointThree = true;  
                
           
        if(other.right > this.x &&
           other.right < this.right &&
           other.bottom > this.y &&
           other.bottom < this.bottom) pointFour = true;
           
           
        return pointOne && pointTwo && pointThree && pointFour;
    }
    
    public bool ContainsPoint(BSPRectangle other)
    {
        return (
            other.x > this.x &&
            other.x < this.x + this.width &&
            other.y > this.y &&
            other.y < this.y + this.height
            );
    }
	
	#endregion
	
}

public struct BSPPoint
{
	public int x;
	public int y;
}
